﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
using namespace std;

typedef unsigned long long ULL;

const int N = 100007, X = 131;
ULL hash_code[N];

int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		int m, n, q;
		scanf("%d %d", &n, &m);
		for (int i = 1; i <= n; i++)
		{
			char val[N];
			scanf("%s", val + 1);
			//复制一次
			for (int j = 1; j <= m; j++)
			{
				val[m + j] = val[j];
			}
			//找到最小表示法的位置
			//通过比较两个子串找到更合适的
			int p1 = 1, p2 = 2, k = 0;
			while (p1 <= m && p2 <= m)
			{
				//相同的跳过
				for (k = 0; k < m && val[p1 + k] == val[p2 + k]; k++);
				/*if (k == m)
				{
					break;
				}*/
				if (val[p1 + k] > val[p2 + k])
				{
					p1 = p1 + k + 1;
				}
				else
				{
					p2 = p2 + k + 1;
				}
				if (p1 == p2)
				{
					//比较的两个子串不能是相同的
					p1++;
				}
			}
			int pos = min(p1, p2);
			//计算该子串的哈希值(即该字符串的最小表示值)
			hash_code[i] = val[pos];
			for (int j = pos + 1; j < pos + 1 + m; j++)
			{
				hash_code[i] = hash_code[i] * X + val[j];
			}
		}
		scanf("%d", &q);
		while (q--)
		{
			int a, b;
			scanf("%d %d", &a, &b);
			if (hash_code[a] == hash_code[b])
			{
				printf("Yes\n");
			}
			else
			{
				printf("No\n");
			}
		}
	}
	return 0;
}